package com.shangma.cn.commons.valid;

import com.shangma.cn.commons.valid.anno.GenderEnum;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/11  15:02
 * 文件含义:
 */
public class GenderConstraintValidator implements ConstraintValidator<GenderEnum, Integer> {


    private List<Integer> list;

    /**
     * 初始化
     *
     * @param constraintAnnotation
     */

    @Override
    public void initialize(GenderEnum constraintAnnotation) {
        //初始化时 获取用户指定的值
        int[] values = constraintAnnotation.values();
        list = CollectionUtils.arrayToList(values);


    }

    //是够验证成功就在这里
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return list.contains(value);
    }
}
